package cn.wolfcode.luowowo.listener;

import cn.wolfcode.louwowo.cache.key.RedisKeys;
import cn.wolfcode.louwowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.louwowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.util.BeanUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * redis数据初始化
 * 实现ApplicationListener<ContextRefreshedEvent>,监听容器启动并初始化完成之后的事件
 */
//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{
    @Reference
    private IStrategyDetailService detailService;
    @Reference
    private IStrategyStatisRedisService redisService;

   public void onApplicationEvent(ContextRefreshedEvent event) {
       System.out.println("----------------init start-----------");
       // 初始化攻略统计数据
       List<StrategyDetail> list = detailService.list();
        //拿到每个攻略明细对象
        for (StrategyDetail detail : list) {
            //检查该攻略明细对象是否已经存在redis中
            if(!redisService.isVoExist(detail.getId())){ //如果不存在redis中，则初始化到redis中
                //攻略明细对象转换成vo对象
                StrategyStatisVO vo = new StrategyStatisVO();
                BeanUtil.copy(vo, detail);
                //属性名不对应的属性另外设置
                vo.setStrategyId(detail.getId());
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                //将vo对象存到redis中
                redisService.setStatisVo(vo);
            }
            //zset中没有该vo对象的时候，初始化国内、海外排名数据
            if(!redisService.isZsetVoExsit(RedisKeys.STRATEGY_STATIS_SORT.getPrefit(),
                    RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()))){
                redisService.addScore(RedisKeys.STRATEGY_STATIS_SORT.getPrefit(),
                        RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()),
                        detail.getThumbsupnum() + detail.getFavornum());

            }
            //zset中没有该vo对象的时候，初始化热门攻略排名数据
            if(!redisService.isZsetVoExsit(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefit(),
                    RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()))){
                redisService.addScore(RedisKeys.STRATEGY_STATIS_HOT_SORT.getPrefit(),
                        RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()),
                        detail.getViewnum() + detail.getReplynum());
            }
            //zset中没有该vo对象的时候，初始化国内热门攻略排名数据
            if(!redisService.isZsetVoExsit(RedisKeys.STRATEGY_STATIS_CHINA_HOT_SORT.getPrefit(),
                    RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()))){
                redisService.addScore(RedisKeys.STRATEGY_STATIS_CHINA_HOT_SORT.getPrefit(),
                        RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString()),
                        detail.getViewnum());
            }
        }
       System.out.println("----------------init end-----------");
   }
}
